#include "pcmodel.h"
#include <thread>
#include <iostream>
#include <chrono>
#include <random>
#include <vector>

std::random_device rd;
std::default_random_engine engine(rd());
std::uniform_int_distribution<int> dis_pro(3000, 8000);
std::uniform_int_distribution<int> dis_con(6000, 7000);

PCModel pool;

void producer(int this_th, int max_th) {
	int id = this_th;
	while (true) {
		std::this_thread::sleep_for(std::chrono::milliseconds(dis_pro(engine)));
		pool.put_in(id);
		std::cout << "thread[" << std::this_thread::get_id() 
			<< "] Produced id (" << id << ")." << std::endl;
		id += max_th;
	}
}

void consumer()
{
	int id = -1;
	while (true) {
		std::this_thread::sleep_for(std::chrono::milliseconds(dis_con(engine)));
		id = pool.try_get_out();
		if (-1 == id) {
			std::cout << "thread[" << std::this_thread::get_id()
				<< "get timeout !" << std::endl;
		}
		else {
			std::cout << "thread[" << std::this_thread::get_id()
				<< "] Consumed id (" << id << ")." << std::endl;
		}
	}
}

int main() {
	const int num_pro = 3;
	const int num_con = 3;
	std::vector<std::thread> ths;

	for (int i = 0; i < num_pro; i++) {
		ths.push_back(std::thread(producer, i, num_pro));
	}
	for (int i = 0; i < num_con; i++) {
		ths.push_back(std::thread(consumer));
	}
	for (size_t i = 0; i < ths.size(); i++) {
		ths[i].join();
	}
	return 0;
}